#!coding:utf8
'''
Created on Aug 17, 2012

@author: join
'''

from jnius import autoclass
from lottery_terminal.common import utils
from subprocess import *

import logging
from lottery_terminal.common.exceptions import NetUnreachableError
LOG = logging.getLogger('terminal')

#def load_lottery_conf():
#    if os.path.exists("/etc/lottery/lottery.conf"):
#        filename = "/etc/lottery/lottery.conf"
#    else:
#        filename = os.path.join(os.path.dirname(os.path.dirname(__file__)),'lottery.conf')
#    c = ConfigParser()
#    try:
#        LOG.error("~~#@##$$")
#        c.read(filename)
#        
#        return dict(c.items('default'))
#    except Exception as e:
#        LOG.exception('load lottery conf occured an exception:%s'%e)
#        pass

        
conf_dict = utils.load_lottery_conf()

agentcode=conf_dict.get("lottery.agentcode")
url=conf_dict.get("lottery.url")
key=conf_dict.get("lottery.key")
username=conf_dict.get("username")
identify=conf_dict.get("identify")
telephone=conf_dict.get('telephone')
#lotterycode = "20"

def get_periods(lotterycode):
    QueryOpenedPeriod = autoclass("com.rbc.http.bean.QueryOpenedPeriod")
    queryOpenedPeriod = QueryOpenedPeriod()
    queryOpenedPeriod.setAgentCode(agentcode)
    queryOpenedPeriod.setLotteryCode(lotterycode)
    res_xml = queryOpenedPeriod.call(url, key)
    return res_xml

def lottery_sd(codes,lotterycode,periodid,batchid):
    try:
        LOG.info("codes:%s,lotterycode:%s,periodid:%s"%(codes,lotterycode,periodid))
        LotteryTicketSaleNew = autoclass("com.rbc.http.bean.LotteryTicketSaleNew")
        bean = LotteryTicketSaleNew()
        
        bean.setAgentCode(agentcode)
        bean.setLotteryCode(lotterycode)
        bean.setPeriodID(periodid)
        bean.setBatchID(batchid)
        
        bean.setIdCardNo(identify)
        bean.setRealName(username)
    
        for code in codes:
            idx,ltcode = code.split("_")
            bean.add(idx,ltcode.strip())
        LOG.info('jiaoyicaipiaoxinxi:%s'%(bean.toXmlString()))    
        resXml = bean.call(url,key);
        if bean.getErrCode()=='9999':
            raise NetUnreachableError()
        return resXml,batchid
    except Exception as e:
        LOG.exception("exception in lottery_sd:%s"%e)

#codes.append("1-1_1,2,3:1:1:1")
#codes.append("1-4_6:1:1:1")


def queryCastResult(lotterycode,batchid):
    QueryCastResult = autoclass("com.rbc.http.bean.QueryCastResult")
    
    bean = QueryCastResult();
    bean.setAgentCode(agentcode);
    bean.setLotteryCode(lotterycode);
    bean.setBatchID(batchid);
    
    resXml = bean.call(url,key);
    
    return resXml

#queryCastResult('20','06b42f8cec3711e1844654e6fc72b618')

def queryAwardCode(lotterycode,periodid):
    if lotterycode=='06':
        result = None
        try:
            result=Popen(["java","-Dfile.encoding=GBK","MyQueryAwardcode","06",periodid,agentcode,url,key,"code"],stdout=PIPE).communicate()[0]
        except:
            pass
        if result:
            result = result.decode("gbk").encode("utf8")
            return result
    QueryAwardCode = autoclass("com.rbc.http.bean.QueryAwardCode")
    
    bean = QueryAwardCode()
    bean.setAgentCode(agentcode)
    bean.setLotteryCode(lotterycode)
    bean.setPeriodID(periodid)

    resXml = bean.call(url,key)
    return resXml


def queryAwardMoney(lotterycode,periodid):
    if lotterycode=='06':
        result = None
        try:
            result=Popen(["java","-Dfile.encoding=GBK","MyQueryAwardcode","06",periodid,agentcode,url,key,"money"],stdout=PIPE).communicate()[0]
        except:
            pass
        if result:
            result = result.decode("gbk").encode("utf8")
            return result
    QueryAwardMoney = autoclass("com.rbc.http.bean.QueryAwardMoney")
    
    bean = QueryAwardMoney()
    bean.setAgentCode(agentcode)
    bean.setLotteryCode(lotterycode)
    bean.setPeriodID(periodid)

    resXml = bean.call(url,key)
    return resXml

def queryPeriodStat(lotterycode,periodid):
    QueryPeriodStat = autoclass("com.rbc.http.bean.QueryPeriodStat")
    
    bean = QueryPeriodStat()
    bean.setAgentCode(agentcode)
    bean.setLotteryCode(lotterycode)
    bean.setPeriodID(periodid)
    
    resXml = bean.call(url,key)

    return resXml

def queryAwardList(lotterycode,periodid,page=1):
    QueryAwardList = autoclass("com.rbc.http.bean.QueryAwardList")
    bean = QueryAwardList()
    
    bean.setAgentCode(agentcode)
    bean.setLotteryCode(lotterycode)
    bean.setPeriodID(periodid)
    bean.setPage(page)
    resXml = bean.call(url,key)
    
    return resXml


def queryBalance(agentcode):
    '''查看公司额度账户余额接口'''
    QueryBalance = autoclass("com.rbc.http.bean.QueryBalance")
    bean = QueryBalance()
    bean.setAgentCode(agentcode)
    resXml = bean.call(url,key)
    
    return resXml

    
if __name__ == '__main__':
#    queryBalance()
    pass
#    dom = parseString(s)
#    resp_node = dom.getElementsByTagName('Resp')[0]

    
#    codes = []
#    codes.append("1-1_01,02,03,04,05,06|07:1:1:1")
##    codes.append("1-4_01,02,03,04,05,06|10:1:1:1")
#    
#    lottery_sd(codes)
    
    
    